package com.saikei.statstitlebar;

import android.graphics.Bitmap;
import android.os.Build;

import java.util.ArrayList;
import java.util.Iterator;

public class BarMenu {

    private static BarMenu barMenu;
    //菜单的集合列表
    private ArrayList<BarItem> leftMenus;
    private ArrayList<BarItem> rightMenus;

    public static BarMenu getInstance() {
        if (barMenu == null) {
            synchronized (BarMenu.class) {
                if (barMenu == null) {
                    barMenu = new BarMenu();
                }
            }
        }
        return barMenu;
    }

    public BarMenu initWithLeftMenu() {
        if (leftMenus == null) {
            leftMenus = new ArrayList<>();
        }
        return barMenu;
    }

    public BarMenu initWithRightMenu() {
        if (rightMenus == null) {
            rightMenus = new ArrayList<>();
        }
        return barMenu;
    }

    public ArrayList<BarItem> getLeftMenus() {
        return leftMenus;
    }

    public ArrayList<BarItem> getRightMenus() {
        return rightMenus;
    }

    public void leftAdd(String text) {
        if (leftMenus.size() > 1) return;
        remove(leftMenus,text);
        leftMenus.add(new BarItem(text));
    }

    private void remove(ArrayList<BarItem> leftMenus, Object object) {
        Iterator<BarItem> iterator = leftMenus.iterator();
        while (iterator.hasNext()) {
            BarItem barItem = iterator.next();
            if (object instanceof String){
                if (barItem.getText().equals((String) object))
                    iterator.remove();
            }else if (object instanceof  Integer){
                if (barItem.getIcon()==(Integer) object)
                    iterator.remove();
            }else if (object instanceof  Bitmap){
                if (barItem.getBitmap()==(Bitmap) object)
                    iterator.remove();
            }
        }
    }

    public void leftAdd(int icon) {
        if (leftMenus.size() > 1) return;
        remove(leftMenus,icon);
        leftMenus.add(new BarItem(icon));
    }

    public void leftAdd(Bitmap bitmap) {
        if (leftMenus.size() > 1) return;
        remove(leftMenus,bitmap);
        leftMenus.add(new BarItem(bitmap));
    }

    public void rightAdd(String text) {
        if (rightMenus.size() > 2) return;
        remove(rightMenus,text);
        rightMenus.add(new BarItem(text));
    }

    public void rightAdd(int icon) {
        if (rightMenus.size() > 2) return;
        remove(rightMenus,icon);
        rightMenus.add(new BarItem(icon));
    }

    public void rightAdd(Bitmap bitmap) {
        if (rightMenus.size() > 2) return;
        remove(rightMenus,bitmap);
        rightMenus.add(new BarItem(bitmap));
    }


}
